<template>
  <el-container class="layout-container">
    <!-- 侧边栏 -->
    <el-aside :width="isCollapse ? '64px' : '220px'" class="aside">
      <!-- Logo -->
      <div class="logo-container" @click="goHome">
        <img src="@/assets/logo.svg" class="logo-img" />
        <span class="logo-text" v-show="!isCollapse">工作流平台</span>
      </div>

      <!-- 菜单 -->
      <el-scrollbar>
        <el-menu
          :default-active="activeMenu"
          :collapse="isCollapse"
          :unique-opened="true"
          class="menu"
          @select="handleSelect"
        >
          <!-- 首页 -->
          <el-menu-item index="/dashboard">
            <el-icon><HomeFilled /></el-icon>
            <template #title>首页</template>
          </el-menu-item>

          <!-- 我的流程 -->
          <el-sub-menu index="/workflow">
            <template #title>
              <el-icon><Document /></el-icon>
              <span>我的流程</span>
            </template>
            <el-menu-item index="/workflow/todo">
              <template #title>
                <el-icon><Timer /></el-icon>
                <span class="menu-item-title">我的待办</span>
              </template>
            </el-menu-item>
            <el-menu-item index="/workflow/done">
              <template #title>
                <el-icon><Check /></el-icon>
                <span class="menu-item-title">我的已办</span>
              </template>
            </el-menu-item>
          </el-sub-menu>

          <!-- 流程管理 -->
          <el-sub-menu index="/process">
            <template #title>
              <el-icon><SetUp /></el-icon>
              <span>流程管理</span>
            </template>
            <el-menu-item index="/process/category">
              <template #title>
                <el-icon><Folder /></el-icon>
                <span>流程分类</span>
              </template>
            </el-menu-item>
            <el-menu-item index="/process/design">
              <template #title>
                <el-icon><Edit /></el-icon>
                <span>流程设计</span>
              </template>
            </el-menu-item>
            <el-menu-item index="/process/instance">
              <template #title>
                <el-icon><List /></el-icon>
                <span>流程实例</span>
              </template>
            </el-menu-item>
          </el-sub-menu>

          <!-- 系统管理 -->
          <el-sub-menu index="/system">
            <template #title>
              <el-icon><Setting /></el-icon>
              <span>系统管理</span>
            </template>
            <el-menu-item index="/system/menu">
              <template #title>
                <el-icon><Menu /></el-icon>
                <span>菜单管理</span>
              </template>
            </el-menu-item>
            <el-menu-item index="/system/dict">
              <template #title>
                <el-icon><Collection /></el-icon>
                <span>字典管理</span>
              </template>
            </el-menu-item>
          </el-sub-menu>
        </el-menu>
      </el-scrollbar>

      <!-- 折叠按钮 -->
      <div class="collapse-btn" @click="toggleCollapse">
        <el-icon :size="16">
          <component :is="isCollapse ? 'Expand' : 'Fold'" />
        </el-icon>
        <span v-show="!isCollapse" class="collapse-text">收起菜单</span>
      </div>
    </el-aside>

    <!-- 主内容区 -->
    <el-container class="main-container">
      <!-- 头部 -->
      <el-header class="header">
        <div class="breadcrumb">
          <el-breadcrumb>
            <el-breadcrumb-item v-for="(item, index) in breadcrumbList" :key="index">
              {{ item }}
            </el-breadcrumb-item>
          </el-breadcrumb>
        </div>
        <div class="header-right">
          <el-dropdown>
            <span class="user-info">
              <el-avatar
                :size="32"
                src="https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png"
              />
              <span class="username">Admin</span>
            </span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item>个人信息</el-dropdown-item>
                <el-dropdown-item>修改密码</el-dropdown-item>
                <el-dropdown-item divided>退出登录</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </el-header>

      <!-- 内容区 -->
      <el-main class="main" :class="{ 'process-design': isProcessDesign }">
        <router-view v-slot="{ Component }">
          <transition name="fade" mode="out-in">
            <component :is="Component" />
          </transition>
        </router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script setup lang="ts">
import { ref, computed } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import {
  HomeFilled,
  Document,
  SetUp,
  Setting,
  Timer,
  Check,
  Folder,
  Edit,
  List,
  Menu,
  Collection,
} from '@element-plus/icons-vue'

const router = useRouter()
const route = useRoute()

// 判断是否是流程设计器页面
const isProcessDesign = computed(() => route.path === '/process/design')

// 菜单折叠状态
const isCollapse = ref(false)

// 当前激活的菜单
const activeMenu = computed(() => route.path)

// 面包屑导航
const breadcrumbList = computed(() => {
  return route.matched.map((item) => item.meta.title || item.name)
})

// 切换菜单折叠状态
const toggleCollapse = () => {
  isCollapse.value = !isCollapse.value
}

// 处理菜单选择
const handleSelect = (index: string) => {
  router.push(index)
}

// 返回首页
const goHome = () => {
  router.push('/dashboard')
}
</script>

<style scoped>
.layout-container {
  width: 100%;
  height: 100vh;
}

.aside {
  height: 100vh;
  background-color: #304156;
  transition: width 0.3s cubic-bezier(0.645, 0.045, 0.355, 1);
  position: relative;
  overflow: hidden;
  box-shadow: 2px 0 6px rgba(0, 21, 41, 0.35);
}

.logo-container {
  height: 60px;
  display: flex;
  align-items: center;
  padding: 0 20px;
  background: #2b2f3a;
  overflow: hidden;
  cursor: pointer;
  transition: all 0.3s cubic-bezier(0.645, 0.045, 0.355, 1);
}

.logo-container:hover {
  background-color: #263445;
}

.logo-img {
  width: 32px;
  height: 32px;
  transition: transform 0.3s cubic-bezier(0.645, 0.045, 0.355, 1);
}

.logo-container:hover .logo-img {
  transform: scale(1.1);
}

.logo-text {
  margin-left: 12px;
  color: #fff;
  font-size: 18px;
  font-weight: 600;
  white-space: nowrap;
  opacity: 1;
  transition: all 0.3s cubic-bezier(0.645, 0.045, 0.355, 1);
}

/* 菜单样式 */
:deep(.el-menu) {
  border-right: none;
  background-color: transparent;
}

:deep(.el-menu-item),
:deep(.el-sub-menu__title) {
  height: 50px;
  line-height: 50px;
  color: #bfcbd9;
  padding: 0 20px !important;
  transition: all 0.3s cubic-bezier(0.645, 0.045, 0.355, 1);

  &:hover {
    color: #fff;
    background-color: #263445 !important;
  }

  .el-icon {
    font-size: 18px;
    vertical-align: middle;
    transition: all 0.3s cubic-bezier(0.645, 0.045, 0.355, 1);
  }
}

:deep(.el-menu-item.is-active) {
  color: #409eff !important;
  background-color: #263445 !important;

  &::before {
    content: '';
    position: absolute;
    left: 0;
    top: 0;
    bottom: 0;
    width: 3px;
    background-color: #409eff;
    transition: transform 0.3s;
    transform: scaleY(1);
  }

  .el-icon {
    color: #409eff;
  }
}

:deep(.el-sub-menu) {
  &.is-active {
    > .el-sub-menu__title {
      color: #fff !important;

      .el-icon {
        color: #fff;
      }
    }
  }

  .el-menu-item {
    background-color: #1f2d3d !important;
    padding-left: 48px !important;

    &:hover {
      background-color: #001528 !important;
    }
  }
}

/* 折叠菜单样式优化 */
:deep(.el-menu--collapse) {
  .el-sub-menu__title {
    padding: 0 16px !important;
  }

  .el-menu-item {
    padding: 0 16px !important;
  }

  .el-icon {
    margin: 0;
  }
}

/* 折叠按钮样式优化 */
.collapse-btn {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  height: 50px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  color: #bfcbd9;
  background: #2b2f3a;
  transition: all 0.3s cubic-bezier(0.645, 0.045, 0.355, 1);
  border-top: 1px solid rgba(255, 255, 255, 0.05);
  padding: 0 16px;
  gap: 8px;

  &:hover {
    background: #263445;
    color: #409eff;
  }

  .collapse-text {
    font-size: 12px;
    white-space: nowrap;
  }
}

/* 子菜单项样式优化 */
.menu-item-title {
  flex: 1;
  margin-left: 4px;
  font-size: 14px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  transition: opacity 0.3s;
}

/* 路由过渡动画优化 */
.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.3s cubic-bezier(0.645, 0.045, 0.355, 1);
}

.fade-enter-from,
.fade-leave-to {
  opacity: 0;
}

.main-container {
  height: 100vh;
  flex-direction: column;
  background-color: #f0f2f5;
}

.header {
  height: 50px;
  background-color: #fff;
  border-bottom: 1px solid #dcdfe6;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
}

.breadcrumb {
  display: flex;
  align-items: center;
}

.header-right {
  display: flex;
  align-items: center;
}

.user-info {
  display: flex;
  align-items: center;
  cursor: pointer;
}

.username {
  margin-left: 8px;
  font-size: 14px;
  color: #606266;
}

.main {
  flex: 1;
  padding: 20px;
  overflow-y: auto;
  transition: all 0.3s;
}

/* 优化子菜单图标样式 */
:deep(.el-menu-item) {
  .el-icon {
    margin-right: 12px;
    font-size: 16px;
  }
}

:deep(.el-menu--collapse) {
  .el-menu-item {
    .el-icon {
      margin: 0;
    }
  }
}
</style>
